package com.shanghaionstar.carmanage;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.shanghaionstar.R;

/**
 * Created by Mengzhou Gu on 2016/7/28.
 */
public class CarManageFuelMeterView extends View {
    private Paint  paint,mPointerPaint;
    private Bitmap bitmap;
    private RectF  rectF;

    float sweep;


    public CarManageFuelMeterView(Context context) {

        super(context);
        init();
    }


    public CarManageFuelMeterView(Context context, AttributeSet attrs) {

        super(context, attrs);
        init();
    }

    public CarManageFuelMeterView(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        if (mode == MeasureSpec.UNSPECIFIED || mode == MeasureSpec.AT_MOST)
            setMeasuredDimension((int)rectF.width(),(int)rectF.width()/2 + 15);
    }

    private void init() {
        paint = new Paint();
        paint.setColor(getResources().getColor(com.onstar.cn.weight.R.color.color_fuel_progress));
        paint.setStrokeWidth(getResources().getDimension(com.onstar.cn.weight.R.dimen.round_width));
        paint.setStyle(Paint.Style.STROKE);
        paint.setAlpha(200);

        mPointerPaint = new Paint();
        mPointerPaint.setStrokeWidth(2);
        mPointerPaint.setColor(0xFF18acfe);
        mPointerPaint.setStyle(Paint.Style.STROKE);
        mPointerPaint.setAntiAlias(true);

        bitmap = getZoom(BitmapFactory.decodeResource(getResources(),R.drawable.carmanage_fuel_fuel0), 1.0f, 1.0f);
        //當成一個正方形處理
        rectF=new RectF(0,0,bitmap.getWidth(),bitmap.getHeight());
    }

    private Bitmap getZoom(Bitmap bitmap, float scaleWidth, float scaleHeight) {
        if (bitmap != null) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            Bitmap newBitmap = Bitmap.createBitmap(bitmap,0, 0, width, height,
                    matrix, true);
            return newBitmap;
        } else {
            return null;
        }
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
      //  canvas.drawBitmap(bitmap, HOME_FUEL_HORIZONTAL, HOME_FUEL_VERTICAL, null);
        canvas.drawBitmap(bitmap,0,0,null);
        RectF rectF1 = new RectF(getPaddingLeft(),getPaddingTop(),getMeasuredWidth()-getPaddingRight(),getMeasuredWidth()-getPaddingBottom());
        canvas.drawArc(rectF1, 180, sweep, false, paint);
        drawPointer(canvas);
    }

    private void drawPointer(Canvas canvas){
        Path path = new Path();
        int cx = getMeasuredWidth()/2;
        int cy = getMeasuredWidth()/2;
        int r = cx - 20;
        path.moveTo(cx-2,cy-3);
        Log.d("drawPointer...",cx + "," + cy);
        path.lineTo((float) (cx + r * Math.cos((180+sweep) * Math.PI / 180))
                , (float) (cy + r * Math.sin((180+sweep) * Math.PI / 180)));
//        path.lineTo(150,150);

        canvas.drawPath(path, mPointerPaint);
    }

    public void playAnimator(float inputSweep) {
        if (inputSweep > 1 || inputSweep < 0)
            throw new IllegalArgumentException("输入的角度不在表盘区间内");
        ValueAnimator frontEndExtend = ValueAnimator.ofFloat(0, inputSweep);
        //        frontEndExtend.setDuration(round_animation / animSteps / 2);
        frontEndExtend.setInterpolator(new DecelerateInterpolator(1));
        frontEndExtend.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                sweep = (Float) animation.getAnimatedValue()*180;
                invalidate();
            }
        });
        frontEndExtend.setDuration(400);
        frontEndExtend.start();
    }

}
